package com.ssm.controller;
import com.alibaba.fastjson.JSONObject;
import com.ssm.model.User;
import com.ssm.service.UserService;
import com.ssm.unity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by xieyingze on 2017/12/30.
 */

@Controller
@RequestMapping("/user")
public class UserController {

    //自动注入业务层的userService类
    @Autowired
    UserService userService;

    /**
     *  APP端 注册接口
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject register(User user, HttpServletRequest request) {

        ResponseResult result = new ResponseResult();
        result.setVersion("v1.1");

        //调用isRegisted方法来验证是否是注册用户
        boolean isRegister = userService.isRegisted(user.getUser_phone());

        if (isRegister) {
            result.setStatusCode("failure");
            result.setInvoking("0004");
            result.setMessage("该手机号已被注册");

        } else {
            boolean registerSuccess = userService.insertUserSuccess(user);
            if (registerSuccess) {
                result.setStatusCode("success");
                result.setInvoking("0000");
                result.setMessage("注册成功");
            } else  {
                result.setStatusCode("failure");
                result.setInvoking("0004");
                result.setMessage("操作失败");
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);

        return jsonObject;
    }

    /**
     * APP端 登录接口
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject login(User user, HttpServletRequest request) {

        ResponseResult result = new ResponseResult();
        HashMap disposeResult = new HashMap();
        result.setCurrentVersion();

        //调用isRegisted方法来验证是否是注册用户
        boolean isRegister = userService.isRegisted(user.getUser_phone());

        if (!isRegister) {
            result.setStatusCode("failure");
            result.setInvoking("0004");
            result.setMessage("该手机号还没有进行注册");

        } else {
            boolean isLogin = userService.isLogin(user.getUser_phone(), user.getUser_password());
            if (!isLogin) {
                result.setStatusCode("failure");
                result.setInvoking("0004");
                result.setMessage("密码错误");
            } else {
                //更新最新设备uuid和登录时间
                boolean updateLoginInfoSuccess = userService.updateLoginInfo(user);

                if (updateLoginInfoSuccess) {
                    result.setStatusCode("success");
                    result.setInvoking("0000");
                    result.setMessage("登录成功");
                    result.setDisposeResult(disposeResult);
                    disposeResult.put("user_uuid", userService.queryUserUUIDByUserPhone(user.getUser_phone()));
                    disposeResult.put("user_phone", user.getUser_phone());
                } else {
                    result.setStatusCode("failure");
                    result.setInvoking("0004");
                    result.setMessage("请求操作失败");
                }
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }

    /**
     * PC端 登录
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("/webLogin")
    public ModelAndView webLogin(User user, HttpServletRequest request) {
        //调用login方法来验证是否是注册用户
        boolean loginType = userService.isLogin(user.getUser_phone(), user.getUser_password());

        if (loginType) {
            //如果验证通过,则将用户信息传到前台
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("user", user);
            modelAndView.setViewName("success");
            return modelAndView;
        } else  {
            //若不对,则将错误信息显示到错误页面
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("message", "用户名或密码错误");
            modelAndView.setViewName("error");
            return modelAndView;
        }
    }


}
